package com.study.controller;

import com.github.pagehelper.PageInfo;
import com.study.Utils.UUIDUtils;
import com.study.entity.AppCategory;
import com.study.entity.AppInfo;
import com.study.entity.DataDictionary;
import com.study.entity.DataUtil;
import com.study.service.IAppCategoryService;
import com.study.service.IAppInfoService;
import com.study.service.IDataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by guanxy on 2017/10/19.
 */
@Controller
@RequestMapping("app")
public class AppInfoController {

    @Autowired
    private IAppInfoService appInfoService;
    @Autowired
    private IAppCategoryService appCategoryService;
    @Autowired
    private IDataDictionaryService dataDictionaryService;

    @RequestMapping("list")
    public String list(Model model, AppInfo appInfo,
                       @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "size",required = false,defaultValue = "2")Integer size){
        List<AppInfo> list = appInfoService.findList(appInfo,page,size);    //调用查找方法
        PageInfo<AppInfo> pageInfo = new PageInfo<>(list);  //创建分页对象
        model.addAttribute("pageInfo",pageInfo);    //将pageInfo添加进model
        model.addAttribute("appInfo",appInfo);
        return "app/list";      //跳转list页面
    }

    @RequestMapping("getLevel2")
    @ResponseBody
    public List<AppCategory> getLevel2(Integer pid){    //获取分级json对象

        return appCategoryService.findListByPid(pid);
    }

    @RequestMapping("add")//约定大于配置原则    app/add
    public void addApp(){
    }//页面跳转

    @RequestMapping("doAdd")
    public String doAdd(Model model, AppInfo appInfo, @RequestParam(name = "img",required = false)MultipartFile multipartFile) throws IOException {
        String filePath = UUIDUtils.uuid()+multipartFile.getOriginalFilename();
        multipartFile.transferTo(new File("d:/upload/",filePath));

        appInfo.setImgUrl(filePath);
        appInfo.setSize((double) multipartFile.getSize());

        int i = appInfoService.saveOne(appInfo);
        if(i!=0){
            return "redirect:list";
        }else{
            model.addAttribute("appInfo",appInfo);
            return "app/add";   //转发到添加页面
        }
    }

    @RequestMapping("dataDictionary")
    @ResponseBody
    public List<DataDictionary> dataDictionary(DataDictionary dataDictionary){
        return dataDictionaryService.findList(dataDictionary);
    }

    @RequestMapping("update")
    public String update(Model model,Integer appId){
        AppInfo appInfo = appInfoService.findOneById(appId);
        model.addAttribute("appInfo",appInfo);
        return "app/update";
    }

    @RequestMapping("doUpdate")
    public String doUpdate(Model model,AppInfo appInfo, @RequestParam(name = "img", required = false) MultipartFile multipartFile) throws IOException {
        String filePath = UUIDUtils.uuid() + multipartFile.getOriginalFilename();//获取文件名
        multipartFile.transferTo(new File("d:/upload/",filePath));//保存文件

        appInfo.setImgUrl(filePath);//设置文件路径
        appInfo.setSize((double) multipartFile.getSize());//设置文件大小


        int i = appInfoService.updateOne(appInfo);
        if(i!=0){
            return "redirect:list";//重定向到app列表页面
        }else {
            model.addAttribute("appInfo", appInfo);//用于数据回显
            return "app/update";//转发到添加页面
        }
    }
    @RequestMapping("shelf")
    @ResponseBody
    public DataUtil hitShelves(Integer appId, String todo){
        DataUtil data=new DataUtil();
        switch (todo){
            case "open":
                appInfoService.hitShelves(appId);
                data.setErrorCode(0);
                data.setResultMsg("success");
                break;
            case "close":
                appInfoService.offShelves(appId);
                data.setErrorCode(0);
                data.setResultMsg("success");
                break;
        }

        return data;
    }
}
